package com.zzyl.nursing.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.constant.Constants;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.core.redis.RedisCache;
import com.zzyl.nursing.domain.NursingProject;
import com.zzyl.nursing.domain.PageResult;
import com.zzyl.nursing.dto.OrdersNursingProjectDto;
import com.zzyl.nursing.mapper.NursingProjectMapper;
import com.zzyl.nursing.service.INursingProjectService;
import com.zzyl.nursing.vo.NursingProjectVo;
import com.zzyl.nursing.vo.OrdersNursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 护理项目Service业务层处理
 * 
 * @author alexis
 * @date 2024-12-30
 */
@Service
public class NursingProjectServiceImpl extends ServiceImpl<NursingProjectMapper, NursingProject> implements INursingProjectService
{
    @Autowired
    private NursingProjectMapper nursingProjectMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询护理项目
     * 
     * @param id 护理项目主键
     * @return 护理项目
     */
    @Override
    public NursingProject selectNursingProjectById(Long id)
    {
        return nursingProjectMapper.selectById(id);
    }

    /**
     * 查询护理项目列表
     * 
     * @param nursingProject 护理项目
     * @return 护理项目
     */
    @Override
    public List<NursingProject> selectNursingProjectList(NursingProject nursingProject)
    {
        return nursingProjectMapper.selectNursingProjectList(nursingProject);
    }

    /**
     * 新增护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int insertNursingProject(NursingProject nursingProject)
    {
        int row = nursingProjectMapper.insert(nursingProject);
        // 缓存到redis
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_PROJECT_PREFIX);
        } catch (Exception e) {
            log.error("新增护理项目后删除Redis缓存失败");
        }
        return row;
    }

    /**
     * 修改护理项目
     * 
     * @param nursingProject 护理项目
     * @return 结果
     */
    @Override
    public int updateNursingProject(NursingProject nursingProject)
    {
        int row = nursingProjectMapper.updateById(nursingProject);
        // 删除redis缓存
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_PROJECT_PREFIX);
        } catch (Exception e) {
            log.error("修改护理项目后删除Redis缓存失败");
        }
        return row;
    }

    /**
     * 批量删除护理项目
     * 
     * @param ids 需要删除的护理项目主键
     * @return 结果
     */
    @Override
    public int deleteNursingProjectByIds(Long[] ids)
    {
        int row = nursingProjectMapper.deleteBatchIds(Arrays.asList(ids));
        // 删除redis缓存
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_PROJECT_PREFIX);
        } catch (Exception e) {
            log.error("删除Redis缓存失败");
        }
        return row;
    }

    /**
     * 删除护理项目信息
     * 
     * @param id 护理项目主键
     * @return   结果
     */
    @Override
    public int deleteNursingProjectById(Long id)
    {
        int row = nursingProjectMapper.deleteById(id);
        // 删除redis缓存
        try {
            redisCache.deleteObject(Constants.CACHE_NURSING_PROJECT_PREFIX);
        } catch (Exception e) {
            log.error("删除Redis缓存失败");
        }
        return row;
    }

    /**
     * 查询所有护理项目VO列表
     *
     * @return 结果
     */
    @Override
    public List<NursingProjectVo> getAllProjects() {
        List<NursingProjectVo> project = null;
        try {
            project = redisCache.getCacheObject(Constants.CACHE_NURSING_PROJECT_PREFIX);
            if (CollUtil.isNotEmpty(project)){
                return project;
            }
        } catch (Exception e) {
            log.error("从Redis中获取护理项目数据失败");
        }
        try {
            project = nursingProjectMapper.getAllProjects();
        } catch (Exception e) {
            log.error("从数据库中获取护理项目数据失败");
        }

        // 缓存到redis
        try {
            redisCache.setCacheObject(Constants.CACHE_NURSING_PROJECT_PREFIX,project,24 * 60 * 60, TimeUnit.SECONDS);
        } catch (Exception e) {
            log.error("将护理项目数据缓存到Redis失败");
        }
        return project;
    }

    @Override
    public PageResult<OrdersNursingProjectVo> findPage(OrdersNursingProjectDto ordersNursingProjectDto) {
        String name = ordersNursingProjectDto.getName();
        String status = ordersNursingProjectDto.getStatus();
        int pageSize = ordersNursingProjectDto.getPageSize();
        int pageNum = ordersNursingProjectDto.getPageNum();
        Page<OrdersNursingProjectVo> page = new Page<>(pageNum,pageSize);
        Page<OrdersNursingProjectVo> result = nursingProjectMapper.findPage(page, name, status);

        PageResult<OrdersNursingProjectVo> pageResult = new PageResult<>();
        pageResult.setTotal(result.getTotal());
        pageResult.setRows(result.getRecords());
        return pageResult;
    }
    @Override
    public TableDataInfo<NursingProject> pageByNameAndStatus(Integer pageNum, Integer pageSize, String name, Integer status) {
        Page<NursingProject> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<NursingProject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(name != null, NursingProject::getName, name)
                .eq(status != null, NursingProject::getStatus, status);
        page = page(page, queryWrapper);

        TableDataInfo<NursingProject> tableDataInfo = buildTableData(page);
        return tableDataInfo;
    }
    private static TableDataInfo<NursingProject> buildTableData(Page<NursingProject> page) {
        TableDataInfo<NursingProject> tableDataInfo = new TableDataInfo<>();
        tableDataInfo.setRows(page.getRecords());
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setCode(200);
        tableDataInfo.setMsg("查询成功");
        return tableDataInfo;
    }
}
